#!/bin/bash
echo 'DownLoading Code...'
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
source ${baseDirForScriptSelf}/source.sh

# 获取源码,脚本仅提供repo sync方式
code_dir=${work_dir}/OpenHarmony
if [ ! -d ${code_dir} ];then
    mkdir -p ${code_dir}
fi
# 判断是否特定网络环境
unset http_proxy
unset https_proxy
alias curl='curl -s -k'
is_huawei_tmp=`curl -I -m 10 -o /dev/null -s -w %{http_code} w3.huawei.com`
if [ "${is_huawei_tmp}" == "200" ];then
    is_huawei=true
    gitee_domain=https://gitee.com/
else
    is_huawei=false
    gitee_domain=git@gitee.com:
fi
source ${baseDirForScriptSelf}/proxy.sh
sudo chmod -R 777 ${code_dir}
#sudo chown -R ${os_user_name}:${os_user_group_name} ${code_dir}
# 本地repo检查,如果是HW内网,改写repo引导脚本,强制使用python2
repo_file=`which repo 2>&1|grep -v /which`
repo_dir=`dirname ${repo_file:-~/bin/repo}`

if [ ! -d ${repo_dir} ];then
    mkdir -p ${repo_dir}
fi
if [ ! -f ${repo_file:-~/bin/repo} ];then
    curl -s -k https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 >~/bin/repo
    chmod +x ~/bin/repo
    sudo bash -c "echo 'export PATH=~/bin:\$PATH'>/etc/profile.d/repo.sh"
fi
if [ "${is_huawei}" == "true" ];then
    sed -i 's|#!/usr/bin/env python2|#!/usr/bin/env python|g;s|#!/usr/bin/env python|#!/usr/bin/env python2|g' ${repo_file:-~/bin/repo}
fi
source /etc/profile.d/repo.sh
cd ${code_dir}

# 如果存在,重置manifest
if [ -d ${code_dir}/.repo/manifests ];then
    cd  ${code_dir}/.repo/manifests
    git reset --hard;git clean -fd
    cd ${code_dir}
fi
# 防止curl出现ssl证书校验失败错误,默认忽略证书
alias curl='curl -s -k'
if [ "${is_huawei}" == "true" ];then
    echo "======================================================================="
    echo """检测到处于HW内网,使用镜像加速下载,过程中可能需要输入账号密码,请关注执行过程"""
    echo "======================================================================="
    unset http_proxy
    unset https_proxy
    # 为防止误操作提交到gitee,将push替换为内网地址
    git config --global url."https://codehub-y.huawei.com/gitee_mirror/openharmony".pushinsteadof https://gitee.com/
    # 内网可以直接复用代理的账号密码,检查账号密码都不为空直接配置账号密码
    if [ "X${proxy_user}" != "X" -a "X${URL_proxy_passwd}" != "X" ];then
        git config --global --unset url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://gitee.com/openharmony/
        git config --global --unset url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://openharmony.gitee.com/openharmony/
        git config --global --unset url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://gitee.com/openharmony/
        git config --global --unset url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://openharmony.gitee.com/openharmony/
        git config --global url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://gitee.com/openharmony/
        git config --global --add  url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://openharmony.gitee.com/openharmony/
    else
        git config --global url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://gitee.com/openharmony/
        git config --global --add  url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://openharmony.gitee.com/openharmony/
    fi
    # 从内网下载,网络足够好,不加--depth参数
    alias curl='curl -s -k'
    repo init -u ${gitee_domain}openharmony/manifest.git -b master --no-repo-verify --no-clone-bundle
    repo forall -c 'git reset --hard;git clean -fd'
    repo sync -j8
    git config --global --unset url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://gitee.com/openharmony/
    git config --global --unset url."https://${proxy_user}:${URL_proxy_passwd}@codehub-dg-y.huawei.com/gitee_mirror/openharmony/".insteadof https://openharmony.gitee.com/openharmony/
    git config --global --unset url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://gitee.com/openharmony/
    git config --global --unset url.'https://codehub-dg-y.huawei.com/gitee_mirror/openharmony/'.insteadof https://openharmony.gitee.com/openharmony/
fi
# 从外网下载代码,应用下代理配置
source ${baseDirForScriptSelf}/proxy.sh
echo "从OpenHarmony社区同步最新代码"
#repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify
# 社区下载,防止下载失败,使用--depth=1减少下载量
repo init -u ${gitee_domain}openharmony/manifest.git -b master --no-repo-verify --no-clone-bundle --depth=1
repo forall -c 'git reset --hard;git clean -fd'
repo sync -c --no-tags
repo forall -c 'git lfs fetch --all'
repo forall -c 'git lfs pull'


